package com.wqiuqiu.gulimall.coupon.service.impl;

import com.wqiuqiu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.wqiuqiu.gulimall.coupon.service.SeckillSkuRelationService;
import com.wqiuqiu.gulimall.coupon.vo.SecdKillSessionWithSkusVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.Query;

import com.wqiuqiu.gulimall.coupon.dao.SeckillSessionDao;
import com.wqiuqiu.gulimall.coupon.entity.SeckillSessionEntity;
import com.wqiuqiu.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {


    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SecdKillSessionWithSkusVo> getLates3DaySession() {
        List<SecdKillSessionWithSkusVo> vos = new ArrayList<>();
        //查找会话
        String startTime = startTime();
        String endTime = endTime();
        List<SeckillSessionEntity> sessionEntities = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));


        //查找关联的sku信息
        for (SeckillSessionEntity entity : sessionEntities) {
            SecdKillSessionWithSkusVo vo = new SecdKillSessionWithSkusVo();
            vo.setSessions(entity);
            List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", entity.getId()));
            vo.setSkus(relationEntities);
            vos.add(vo);
        }
        return vos;
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);

        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate plusDays = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime start = LocalDateTime.of(plusDays, max);

        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}